<?php
include_once ('class.mysql.php');

class EditOrder
{
    private $comtec_id_order;
    private $operation_type;
    private $comtec_id_responsible;
    private $tasks;
    private $notes;
    private $order_start_date;
    private $order_end_date;
    private $cod_order;
    private $errors;
    private $token;

    public function __construct()
    {
     $this->errors   = array();
        
     $this->comtec_id_order       = $_POST['comtec_id_order'];       
     $this->comtec_id_responsible = $_POST['comtec_id_responsible'];
     $this->operation_type        = $_POST['operation_type'];
     $this->order_start_date      = $_POST['op_year1'].$_POST['op_month1'].$_POST['op_day1'];
     $this->order_end_date	  = $_POST['op_year2'].$_POST['op_month2'].$_POST['op_day2'];        
     $this->tasks                 = $_POST['tasks']; 
     $this->cod_order             = $_POST['cod_order'];   
     $this->notes		  = $_POST['notes'];
     if(strlen($this->notes) > 400)
     {
      $this->notes = substr($this->notes,0,400);   
     }
        
     $this->token = $_POST['token'];
              
    }

    public function process()
    {
	$this->edit();

        return count($this->errors)? 0 : 1;
    }

    public function filter($var)
    {
        return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
    }

    public function filter_num($var)
    {
        return preg_replace('/[^0-9+]/','',$var);
    }

    public function edit()
    {
        $db = new MySQL();
        
        $tsql  = "UPDATE orders SET ";      
        $tsql .= "comtec_id_responsible= $this->comtec_id_responsible , ";           
        $tsql .= "operation_type= $this->operation_type , ";
        $tsql .= "cod_order= '$this->cod_order' , "; 
        $tsql .= "tasks= '$this->tasks' , ";        
        $tsql .= "notes= '$this->notes', ";
        $tsql .= "order_start_date= '$this->order_start_date', ";
        $tsql .= "order_end_date= '$this->order_end_date' ";
	$tsql .= " WHERE comtec_id_order = $this->comtec_id_order ";
        
        $result = $db->query($tsql);

        if($db->affected_rows() < 1)
            $this->errors[] = 'No se puede procesar la edici&oacute;n';
    
    }

    public function show_errors()
    {
        $msg = "";
        foreach($this->errors as $key=>$value)
            $msg = $msg . $value . "\n";
        return $msg;
    }

    public function valid_token()
    {
        if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
            $this->errors[] = 'Registro inv&aacute;lido';

        return count($this->errors)? 0 : 1;
    }
}
?>
